#!/bin/bash
#usage: updatekernel 6.7.1
#by 229 26/January/2024 Saturday
#使用Slackware官方脚本编译内核的流程
if [ $# -ne 1 ]
then
	echo Usage: $(basename $0) kernel version
	echo example: $(basename $0) 6.7.1
	echo the latest kernel version
	exit
fi
#update the kernel of slackware current
version=$1
kernelpath='/media/sda1/os/linux/kernel/k/'
kernel='/media/sda1/os/linux/kernel/'
#1.从www.kernel.org网站下载最新内核
#cd $(dirname $0) ; CWD=$(pwd)
cd ${kernelpath} ; CWD=$(pwd)
wget -c https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${version}.tar.sign
wget -c https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-${version}.tar.xz
#2.解压内核源码到/usr/src/目录
sudo tar xJvf linux-${version}.tar.xz  -C /usr/src/
#3.链接目录linux-${version}到linux
cd /usr/src
sudo rm -rf linux
sudo ln -s linux-${version}  linux
#4.配置内核
#使用slackware64-current/source/k/kernel-configs/下 config-generic-6.6.12.x64和config-huge-6.6.12.x64配置文件
cd /usr/src/linux
#复制配置文件到当前目录
cp -v ${kernelpath}"kernel-configs/config-generic-"*"x64"  .
cp -v ${kernelpath}"kernel-configs/config-huge-"*"x64"  .
ls -alh /usr/src/linux/
echo "Press any key to continue ..."
read
#配置 generic
#配置 huge
make menuconfig
#load两个配置文件，save对应配置文件到source/k/kernel-configs/，只需要命名为`config-generic-${version}.x64`和`config-huge-${version}.x64`两个文件
#保存两个配置文件到 kernel-configs
rm ${kernelpath}"kernel-configs/"*
cp -v config-generic-${version}.x64  ${kernelpath}"kernel-configs/"
cp -v config-huge-${version}.x64  ${kernelpath}"kernel-configs/"
#保存generic为当前配置文件
cp -v config-generic-${version}.x64  .config
#5.编译内核
cd ${CWD}
sh build-all-kernels.sh
#生成的所有内核文件在/tmp/output-x86_64-${version}目录下，并且脚本已经自动安装好内核了。
#6.复制保留内核
mv -v linux-${version}.tar.xz ..
mv -v linux-${version}.tar.sign ..
cd ${kernel}
cp -r -v /tmp/output-x86_64-${version} .
chown -R ooo:ooo output-x86_64-${version}
chown ooo:ooo linux-${version}.tar.xz
chown ooo:ooo linux-${version}.tar.sign
cd ${CWD}
